<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible"
    append-to-body
  >
    <el-form
      :model="dataForm"
      :rules="dataRule"
      ref="dataForm"
      label-width="80px"
    >
      <el-form-item label="类型" prop="type">
        <el-radio-group v-model="dataForm.type">
          <el-radio
            v-for="(type, index) in typeList"
            :label="index"
            :key="index"
            >{{ type }}</el-radio
          >
        </el-radio-group>
      </el-form-item>

      <el-form-item label="名称" prop="name">
        <el-input v-model="dataForm.name" placeholder="名称"></el-input>
      </el-form-item>

      <el-form-item
        v-if="dataForm.type === 1"
        label="上级菜单"
        prop="parentName"
      >
        <el-popover
          ref="menuListPopover"
          placement="bottom-start"
          trigger="click"
        >
          <el-tree
            :data="menuList"
            :props="menuListTreeProps"
            node-key="id"
            ref="menuListTree"
            @current-change="menuListTreeCurrentChangeHandle"
            default-expand-all
            highlight-current
            :expand-on-click-node="false"
          >
          </el-tree>
        </el-popover>
        <el-input
          v-model="dataForm.parentName"
          v-popover:menuListPopover
          placeholder="点击选择上级菜单"
          class="menu-list__input"
        />
      </el-form-item>

      <el-form-item label="菜单路由" prop="url">
        <el-input v-model="dataForm.url" placeholder="菜单路由"></el-input>
      </el-form-item>

      <el-form-item label="菜单图标" prop="icon">
        <el-popover
          ref="iconListPopover"
          placement="bottom-start"
          trigger="click"
        >
          <i
            v-for="item in iconList"
            :key="item"
            @click="iconActiveHandle(item)"
            :class="item"
          />
        </el-popover>
        <el-input
          v-model="dataForm.icon"
          v-popover:iconListPopover
          readonly
          placeholder="点击选择菜单图标名称"
          class="icon-list__input"
        ></el-input>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button size="small" @click="visible = false">取消</el-button>
      <el-button size="small" type="primary" @click="dataFormSubmit()"
        >确定</el-button
      >
    </span>
  </el-dialog>
</template>

<script>
import { editMenu } from "@/api/sys-menu"
export default {
  data() {
    return {
      visible: false,
      typeList: ["目录", "菜单"],
      dataForm: {
        id: "",
        type: 0,
        name: "",
        parentId: 0,
        parentName: "",
        url: "",
        icon: "",
      },
      dataRule: {
        name: [
          { required: true, message: "菜单名称不能为空", trigger: "blur" },
        ],
      },
      menuList: [],
      menuListTreeProps: {
        label: "name",
        children: "children",
      },
      iconList: ["el-icon-folder-opened", "el-icon-document"],
    }
  },
  methods: {
    init(id = null) {
      Object.assign(this.$data, this.$options.data()) //清空组件状态
      this.visible = true //展示对话框
      this.menuList = JSON.parse(JSON.stringify(this.$parent.dataList))

      // 递归查找
      let routePath = null
      function getPath(val, menuItems) {
        for (const item of menuItems) {
          if (item.id === val) {
            return (routePath = item)
          } else {
            if (item.hasOwnProperty("children")) {
              getPath(val, item.children)
            }
          }
        }
        return routePath
      }

      if (id !== null) {
        // 根据id递归查找  修改 需深拷贝
        const res = getPath(id, this.$parent.dataList)
        this.dataForm = {
          ...this.dataForm,
          ...JSON.parse(JSON.stringify(res)),
        }
      }
      this.dataForm.id = id
    },
    // 菜单树选中
    menuListTreeCurrentChangeHandle(data, node) {
      this.dataForm.parentName = data.name
      this.dataForm.parentId = data.id
    },
    // 图标选中
    iconActiveHandle(iconName) {
      this.dataForm.icon = iconName
    },
    // 表单提交
    dataFormSubmit() {
      this.$refs["dataForm"].validate(valid => {
        if (!valid) return
        // 修改菜单 /nuwa-sys-menu/edit
        const sysMenuReqDto = { ...this.dataForm, appId: this.appId }
        editMenu(sysMenuReqDto)
          .then(res => {
            if (res.code === 200) {
              this.$message({
                message: "操作成功",
                type: "success",
                duration: 1500,
                onClose: () => {
                  this.visible = false
                  this.$emit("refreshDataList")
                },
              })
            } else {
              this.$message.error("操作失败")
            }
          })
          .catch(() => {
            this.$message.error("操作失败")
          })
      })
    },
  },
  computed: {
    appId() {
      return this.$store.state.application.appId
    },
  },
}
</script>

<style lang="scss">
.mod-menu {
  .menu-list__input,
  .icon-list__input {
    > .el-input__inner {
      cursor: pointer;
    }
  }
  &__icon-popover {
    width: 458px;
    overflow: hidden;
  }
  &__icon-inner {
    width: 478px;
    max-height: 258px;
    overflow-x: hidden;
    overflow-y: auto;
  }
  &__icon-list {
    width: 458px;
    padding: 0;
    margin: -8px 0 0 -8px;
    > .el-button {
      padding: 8px;
      margin: 8px 0 0 8px;
      > span {
        display: inline-block;
        vertical-align: middle;
        width: 18px;
        height: 18px;
        font-size: 18px;
      }
    }
  }
  .icon-list__tips {
    font-size: 18px;
    text-align: center;
    color: #e6a23c;
    cursor: pointer;
  }
}
</style>
